package com.company;

import org.apache.commons.io.IOUtils;
import org.json.simple.JSONObject;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.nio.file.Files;


public class Main {

    public static void main(String[] args) throws IOException {
//	    System.out.println("start");
//        // This function Calls LOGIN API to get token for future use
//        String str = getToken("http://202.120.47.213:12345","usr","psw");
//        System.out.println("Get Token : " + str);

        sendFile("http://localhost:12345", "/home/d/Pictures/1.png");
    }

    static String genJson(String name, String psw){
        JSONObject matadata = new JSONObject();
        matadata.put("TimeStamp",123124233);
        matadata.put("Device", "android");
        JSONObject userinfo = new JSONObject();
        userinfo.put("username", name);
        userinfo.put("password", psw);

        JSONObject data = new JSONObject();
        data.put("mataData",matadata);
        data.put("userinfo", userinfo);
        return data.toJSONString();
    }

    static String getToken(String surl, String name, String psw) throws IOException {
        String jsonstr = genJson(name, psw);
        URL url  = new URL(surl + "/login");
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("POST");
        conn.setDoOutput(true);
        conn.setRequestProperty("Content-Type","application/json");
        conn.getOutputStream().write(jsonstr.getBytes());

        String ostr = IOUtils.toString(conn.getInputStream());
        System.out.println(ostr);

        org.json.JSONObject outjson = new org.json.JSONObject(ostr);
        return outjson.getString("token");
    }

    static void sendFile(String surl, String filename) throws IOException {
        URL url = new URL(surl + "/upload");
        String charset = "UTF-8";
        String CRLF = "\r\n";
        String boundary = Long.toHexString(System.currentTimeMillis());
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setDoOutput(true);
        conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
        conn.setRequestMethod("POST");

        OutputStream outputStream = conn.getOutputStream();
        PrintWriter writer = new PrintWriter(new OutputStreamWriter(outputStream,charset), true);

        // sent a binary file
        File binaryFile = new File(filename);
        writer.append("--" + boundary).append(CRLF);
        writer.append("Content-Disposition: form-data; name=\"binaryFile\"; filename=\"" + binaryFile.getName() + "\"").append(CRLF);
        writer.append("Content-Type: " + URLConnection.guessContentTypeFromName(binaryFile.getName())).append(CRLF);
        writer.append("Content-Transfer-Encoding: binary").append(CRLF);
        writer.append(CRLF).flush();
        Files.copy(binaryFile.toPath(), outputStream);
        outputStream.flush();
        writer.append(CRLF).flush();
        // End of multipart/form-data.
        writer.append("--" + boundary + "--").append(CRLF).flush();
        int respcode = conn.getResponseCode();
        System.out.println("RespCode:" + respcode);
        if (respcode == 200){
            String ostr = IOUtils.toString(conn.getInputStream());
            System.out.println(ostr);
        }
    }
}


